package com.twq.filter;


import javax.servlet.*;
import java.io.IOException;

public class CharacterEncodingFilter implements Filter {

    @Override//初始化：web服务器启动就初始化
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");

        System.out.println("filter执行前");
        chain.doFilter(request,response);//让请求继续走，如果不写，程序在这里被拦截停止
        System.out.println("filter执行后");
    }

    @Override//销毁：web服务器关闭销毁
    public void destroy() {
    }
}
